﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace XmlEmbed
{
	public class EmbedClassGen
	{
		// ============================================================
		public EmbedClassGen()
		{

		}
		// ============================================================
		public string GenerateEmbedClass(string xml_path)
		{
			List<string> xmls = GetXmlsList(xml_path);

			StringBuilder sb = new StringBuilder();
			sb.AppendLine("/* =======================================");
			sb.AppendLine(" * ### DO NOT EDIT THIS FILE ###");
			sb.AppendLine(" * FILE WAS AUTOGENERATED BY XmlEmbed v 1.0");
			sb.AppendLine(" * ======================================= */");

			sb.AppendLine("package");
			sb.AppendLine("{");
			sb.AppendLine("	public dynamic class XmlEmbed");
			sb.AppendLine("	{");

			//sb.AppendLine("		public static function Init():void");
			//sb.AppendLine("		{");
			foreach (var file in xmls)
			{
				// пути нужно указывать относительно исполнимого SWF, а не относительно XmlEmbed:
				sb.AppendLine(String.Format("		[Embed(source=\"../xml/{0}\", mimeType=\"application/octet-stream\")]", file));
				sb.AppendLine(String.Format("		public var Embed_{0}:Class;", file.Substring(0,file.Length-".xml".Length) ) );
			}
			//sb.AppendLine("		}");

			sb.AppendLine("	}");
			sb.AppendLine("}");

			return sb.ToString();
		}
		// ============================================================
		private List<string> GetXmlsList(string xml_path)
		{
			string[] files = Directory.GetFiles(xml_path, "*.xml", SearchOption.TopDirectoryOnly);
			List<string> names = new List<string>(files);

			// need to remove path:
			for (int i = 0; i < names.Count; i++)
			{
				var str = names[i];
				FileInfo fi = new FileInfo(str);
				names[i] = fi.Name;
			}

			return names;
		}
		// ============================================================
	}
}
